package com.sq.partner.manager.service.impl;


import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.sq.partner.manager.common.Config;
import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.dao.GatewayEinvoiceDao;
import com.sq.partner.manager.dao.IGatewayPolicyDao;
import com.sq.partner.manager.mybatis.model.GatewayEinvoice;
import com.sq.partner.manager.mybatis.model.GatewayPolicy;
import com.sq.partner.manager.service.EinvicePrintService;
import com.sq.partner.manager.service.FacadeAppApplicantService;
import com.sq.partner.manager.service.FacadeAppFormService;
import com.sq.partner.manager.util.FreeMarkerUtil;
import com.sq.partner.manager.util.HttpConnectorUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.StrUtil;


/**
 * 发票下载
 * @author duguangwei
 *
 */
@Service
public class EinvicePrintServiceImpl  extends BaseServiceImpl implements EinvicePrintService {
	private static Logger logger = LogUtil.getLog(EinvicePrintServiceImpl.class);
	@Resource
	FacadeAppFormService facadeAppFormService;
	@Resource
	IGatewayPolicyDao gatewayDao;
	@Resource
	FacadeAppApplicantService  facadeAppApplicantService;
	@Resource
	private GatewayEinvoiceDao einvoicedao;
	
	@Override
	public Map<String, Object> reqEinvice(Map<String, Object> dataMap) {
		 Map<String, Object> reMap=new HashMap<String, Object>();
		try {
			GatewayPolicy policy = gatewayDao.queryGatewayPolicyByPolno(dataMap.get("polNo").toString());
			BigDecimal AMT=policy.getPremium();
			Map<String,Object> map =new HashMap<String,Object>();
			map.put("BUYERNAME", dataMap.get("einvoiceName"));
			map.put("BUYERNO", dataMap.get("taxNo") );
			map.put("MOBILE", dataMap.get("phoneNo"));
			map.put("EMAIL", dataMap.get("email"));
			map.put("PARTNERCODE", policy.getPartnerNo());
			map.put("serialnumber", generateSerialNumber("fEin9"));
			map.put("AMT", AMT);
			map.put("POLNO", dataMap.get("polNo"));
			String templates = Config.getProp(Constants.NLIFE_EINVINCE_PS009);
			String requestXml = FreeMarkerUtil.process(templates, map);
			logger.info("请求PS009接口请求报文" + requestXml);
			String responseXml = HttpConnectorUtil.interaction(Config.getProp("pgroup.pushInvoice.url"), requestXml, policy.getPartnerNo(), Constants.GW_CH_TX_PS009);
			logger.info("请求PS009交易系统返回报文" + responseXml);
			/***解析返回报文*/
			if(StrUtil.isEmpty(responseXml)){
				reMap.put("result", 0);
				reMap.put("message", "请求交易系统失败");
				return reMap;
			}else{
				Document document =DocumentHelper.parseText(responseXml);
				Element  root=document.getRootElement();
				String resultcode=root.selectSingleNode("/INSUREQRET/HEAD/RESULTCODE")==null?"":root.selectSingleNode("/INSUREQRET/HEAD/RESULTCODE").getText();//返回结果标识
				if("0000".equals(resultcode)){
					reMap.put("result", 1);
					reMap.put("message", "发票申请成功请稍后下载");
					return reMap;	
				}else {
					String meg=root.selectSingleNode("/INSUREQRET/HEAD/ERRINFO")==null?"":root.selectSingleNode("/INSUREQRET/HEAD/ERRINFO").getText();//返回消息
					reMap.put("result", 0);
					reMap.put("message", "交易系统返回失败:"+meg);
					return reMap;	
				}
			}
		} catch (Exception e) {
			logger.info("PS009接口操作异常:"+e);
			reMap.put("result", 0);
			reMap.put("message", "操作异常"+e);
			return reMap;
		}
	}
	
	@Override
	public GatewayEinvoice geteinvoice(String polno) throws Exception{
		return einvoicedao.queryeinvoicebypolno(polno);
	}
	
	private String generateSerialNumber(String prodType) throws Exception {
		Calendar now = Calendar.getInstance();
		String suffixStr = now.get(Calendar.YEAR) + "" + now.get(Calendar.MONTH) + "" + now.get(Calendar.DAY_OF_MONTH) + "" + now.getTimeInMillis();
		return prodType + "@" + suffixStr;
	}
	
}
